using System.Diagnostics;
using System.IO;
using System.Text;

namespace Catastrophe.Common.Tools
{
	public class CmdHelper
	{
		private static string CmdPath = "C:\\Windows\\System32\\cmd.exe";

		public static void RunCmd(string cmd, out string output)
		{
			cmd = cmd.Trim().TrimEnd('&') + "&exit";
			using Process process = new Process();
			process.StartInfo.FileName = CmdHelper.CmdPath;
			process.StartInfo.UseShellExecute = false;
			process.StartInfo.RedirectStandardInput = true;
			process.StartInfo.RedirectStandardOutput = true;
			process.StartInfo.RedirectStandardError = true;
			process.StartInfo.CreateNoWindow = true;
			process.Start();
			process.StandardInput.WriteLine(cmd);
			process.StandardInput.AutoFlush = true;
			process.StandardInput.WriteLine("exit");
			output = process.StandardOutput.ReadToEnd();
			process.WaitForExit();
			process.Close();
		}

		public static string RunCmdBat(string filePath)
		{
			string cmd = File.ReadAllText(filePath, Encoding.GetEncoding("GBK"));
			CmdHelper.RunCmd(cmd, out var output);
			return output;
		}
	}
}
